<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.js'></script>
</head>

<body>
     <!-- 
      在vue渲染的过程中 diff算法是一直在工作的,但是diff算法的比较是默认按照顺序依次比较的
      如果说我们逆序添加数据,则很可能diff算法的对比错位了
      所以我们在列表渲染的时候要添加一个唯一的key,diff算法的比较就变成了key对应的新旧虚拟DOM的比较
     -->
    <div id='app'>
        <button @click="add">在前面添加一个人</button>
        <button @click="add1">在后面添加一个人</button>
        <ul>
            <li v-for="item in persons" :key="item.id">
                姓名：{{item.name}} --- 年龄 {{item.age}}
            </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        const vm = new Vue({
            el: '#app',
            data() {
                return {
                    persons:[
                        {id:'001',name:'laowang',age:16},
                        {id:'002',name:'laowang',age:33},
                        {id:'003',name:'laowang',age:22}
                    ]
                }
            },
            methods:{
                add(){
                    this.persons=[
                        {id:Date.now(),name:'laoli',age:28},
                        ...this.persons,
                    ]
                },
                add1(){
                    this.persons=[
                        ...this.persons,
                        {id:Date.now(),name:'laolifff',age:18},
                    ]
                }
            }
        })
    </script>
</body>

</html>